package com.fyzk.activity.setting;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.fyzk.bean.FeedbackListBean;
import com.tyjc.lottery.R;
import com.fyzk.utils.ScreenUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by fanxs11 on 17/5/15.
 */

public class FeedbackListAdapter extends BaseAdapter {

    private Context context;
    private List<FeedbackListBean> infors = new ArrayList<>();
    private String servicePhone;

    public FeedbackListAdapter(Context context, List<FeedbackListBean> infors, String servicePhone) {
        this.context = context;
        this.infors = infors;
        this.servicePhone = servicePhone;
    }

    @Override
    public int getCount() {

        return infors.size();
    }

    @Override
    public Object getItem(int position) {

        return infors.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        FeedbackListBean info = infors.get(position);
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_feedback,
                    null);

            holder.tishi_tv = (TextView) convertView.findViewById(R.id.feedback_item_tishi);
            holder.type_iv = (ImageView) convertView.findViewById(R.id.feedback_item_type_iv);
            holder.time_tv = (TextView) convertView.findViewById(R.id.feedback_item_time);
            holder.reply_tv = (TextView) convertView.findViewById(R.id.feedback_item_reply);
            holder.content_tv = (TextView) convertView.findViewById(R.id.feedback_item_content);
            holder.content_iv = (ImageView) convertView.findViewById(R.id.feedback_item_img);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position == 0) {
            holder.tishi_tv.setVisibility(View.VISIBLE);
        } else {
            holder.tishi_tv.setVisibility(View.GONE);
        }

        holder.tishi_tv.setText(servicePhone);

        if (info.getType() == 2) {  // 答
            holder.reply_tv.setVisibility(View.GONE);
            holder.type_iv.setImageDrawable(context.getResources().getDrawable(R.drawable.feedback_answer_icon));
        } else {
            holder.reply_tv.setVisibility(View.VISIBLE);
            if (info.isReply) {
                holder.reply_tv.setTextColor(context.getResources().getColor(R.color.feedback_color_009dff));
                holder.reply_tv.setText("已回复");
            } else {
                holder.reply_tv.setTextColor(context.getResources().getColor(R.color.feedback_color_666666));
                holder.reply_tv.setText("等待回复");
            }
            holder.type_iv.setImageDrawable(context.getResources().getDrawable(R.drawable.feedback_question_icon));
        }

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(new Date(info.getCreate_time() * 1000));
        holder.time_tv.setText(time);
        holder.content_tv.setText(info.getContent());
        int width = info.getWidth();
        int height = info.getHeight();
        if (width > 0 && height > 0) {
            if (width > ScreenUtils.getInstance().getWidth()*1.0/2) {
                width = (int)(ScreenUtils.getInstance().getWidth()*1.0/2);
                height = (int)(width * 1.0 / info.getWidth() * height);
            }
            holder.content_iv.setVisibility(View.VISIBLE);
            holder.content_iv.setLayoutParams(new LinearLayout.LayoutParams(width,height));
            Glide.with(context)
                    .load(info.getFeed_pic())
                    .placeholder(R.color.white)
                    .transition(DrawableTransitionOptions.withCrossFade(1000))// 动画
                    .diskCacheStrategy(DiskCacheStrategy.DATA)    // 缓存：NONE 什么都不缓存，SOURCE 仅仅只缓存原来的全分辨率的图像，RESULT 仅仅缓存最终的图像，即降低分辨率后的（或者是转换后的），ALL 缓存所有版本的图像（默认行为）
                    .override(width,height)     // 图片自定义显示大小
                    .into(holder.content_iv);
        } else {
            holder.content_iv.setVisibility(View.GONE);
        }

        return convertView;
    }

    public void setInfors(List<FeedbackListBean> infors) {
        this.infors = infors;
        this.notifyDataSetChanged();
    }

    class ViewHolder {
        TextView tishi_tv;
        ImageView type_iv;
        TextView time_tv;
        TextView reply_tv;
        TextView content_tv;
        ImageView content_iv;
    }
}
